package Frame.shopSet;

import FCRendering.FCRConstants;
import FCRendering.FCRMaster;
import FCRendering.sw.FCRValues;
import Frame.entity.UnifyShopSayUIEntity;
import VWorldGameStudio.*;
import lombok.Setter;
import Function.FCMutilPlayCache;

import java.util.Vector;

import static FCRendering.FCRMaster.*;

//战车升级改造店-只能升级底盘-打洞，堵住洞，增加弹仓和减少弹仓
public class FunctionUpgradeDipanShop extends AShopFuntion{
    private int OpenHolePrice = 1000;
    private int CloseHolePrice = 2000;
    private int AddShellHole = 500;
    private int delShellHole = 500;

    private Vector<FixTank> needServiceNames = new Vector<>();

    private int showFristIndex = 0;
    @Setter
    private int endI = 0;

    private boolean enableChangeGun1Hole = true;
    private boolean enableChangeGun2Hole = true;
    private boolean enableChangeSEHole = true;

    private boolean enableChangeShellHole = true;//是否支持改造弹仓
    private FixTank selectFixTank = null;//就是选中要升级那个了
    private int selectMode = -1;//1是改造穴，2是改造弹仓

    private Runnable clickItRun = null;
    public FunctionUpgradeDipanShop(){
        buildShopWindow();
    }

    public void buildShopWindow(){
        FCTank tank = tankLib.getTankOfID(player1.getPlayertank1(), player1.getPlayertankHostId());
        if (tank != null && tank.getType().equals("owner")) {
            checkAndAdd(tank);
        }
        for (FCRPlayerLink link : FCMutilPlayCache.getTeamMembers()) {
            if(link.curPlayerHostUID.equals(gameSave.HOST_UID)) {
                FCPlayerInfo p = FCMutilPlayCache.findPlayerByName(link.curPlayerHostUID, link.curPlayerName);
                if (p != null) {
                    tank = tankLib.getTankOfID(p.getPlayertank1(), p.getPlayertankHostId());
                    if (tank != null && tank.getType().equals("owner")) {
                        checkAndAdd(tank);
                    }
                }
            }
        }
        //还有牵引的
        FCPropulsion fc = fcv.findPropulsionByHostId(gameSave.HOST_UID);
        if(fc != null && fc.getPropulsionHostId().equals(gameSave.HOST_UID) && fc.getPropulsionType().equals("tank")
        && ((FCTank)fc).getType().equals("owner")) {
            checkAndAdd((FCTank) fc);
        }

        setEndI(Math.min(needServiceNames.size(), showFristIndex + 6));
        resetUI();
    }

    private void checkAndAdd(FCTank tank) {
        FCItem item = FCRMaster.itemLib.getItemOfID(tank.getChassisID());
        if (item != null) {
            needServiceNames.add(new FixTank(tank.getChassisID(), tank, item.getUpgradeMoney(),
                    langLib.getNameOfId(tank.getName())+"-"+item.getChName()));
        }
    }

    private void resetUI(){
        FCRValues.resetUnifySayUIEntity();
        if(selectFixTank == null) {
            if(needServiceNames.size() == 0){
                FCRValues.leftWindowUI[0].leftText = "抱歉你可能忘记带战车来了吧";
            }else {
                FCRValues.leftWindowUI[0].leftText = "选择要改造的底盘";
                int m = 1;
                for (int i = showFristIndex; i < endI; i++) {
                    FixTank fixTank = needServiceNames.get(i);
                    FCRValues.leftWindowUI[m].leftText = fixTank.text;
                    m++;
                }
            }
        }else {
            FCItem item = FCRMaster.itemLib.getItemOfID(selectFixTank.itemName);

            FCRValues.leftWindowUI[0].leftText = "请选中以下功能";
            if(selectMode == -1) {
                FCItem upgradeitem = FCRMaster.itemLib.getItemOfID(item.getUpgradeTarget());
                if (upgradeitem != null) {
                    FCRValues.leftWindowUI[1].leftText = "可升级成" + upgradeitem.getChName();
                    FCRValues.leftWindowUI[1].rightText = selectFixTank.needFixMoney+"G";
                } else {
                    FCRValues.leftWindowUI[1].leftText = "无法继续升级了";
                }
                FCRValues.leftWindowUI[2].leftText = "改造穴";
                if (enableChangeShellHole) {
                    FCRValues.leftWindowUI[3].leftText = "改造弹仓";
                }
            }else if(selectMode == 1){
                int[] holes = item.getCannonHoles();

                FCRValues.leftWindowUI[1].leftText = "Tips:改造穴会影响底盘重量";
                if(enableChangeGun1Hole) {
                    FCRValues.leftWindowUI[2].leftText = (holes[0]==0?"打开":"堵上")+"主炮穴";
                    FCRValues.leftWindowUI[2].rightText = (holes[0]==0? OpenHolePrice:CloseHolePrice)+"G";
                }else {
                    FCRValues.leftWindowUI[2].leftText = "主炮穴技术无法改造";
                }
                if(enableChangeGun2Hole) {
                    FCRValues.leftWindowUI[3].leftText = (holes[1]==0?"打开":"堵上")+"副炮穴";
                    FCRValues.leftWindowUI[3].rightText = (holes[1]==0? OpenHolePrice:CloseHolePrice)+"G";
                }else {
                    FCRValues.leftWindowUI[3].leftText = "副炮穴技术无法改造";
                }
                if(enableChangeSEHole) {
                    FCRValues.leftWindowUI[4].leftText = (holes[2]==0?"打开":"堵上")+"SE穴";
                    FCRValues.leftWindowUI[4].rightText = (holes[2]==0? OpenHolePrice:CloseHolePrice)+"G";
                }else {
                    FCRValues.leftWindowUI[4].leftText = "SE穴技术无法改造";
                }
            }else if(selectMode == 2){
                FCRValues.rightWindowUI[0].leftText = "改造弹仓会影响底盘重量 - 当前弹仓"+item.getShellCaseNum()+"个";
                FCRValues.leftWindowUI[2].leftText = "增加弹仓";
                FCRValues.leftWindowUI[2].rightText = AddShellHole+"G";
                FCRValues.leftWindowUI[3].leftText = "减少弹仓";
                FCRValues.leftWindowUI[3].rightText = delShellHole+"G";
            }
        }
        FCRValues.rightWindowUI[2].leftText = "改造一下打遍天下.";
        FCRValues.rightWindowUI[3].rightText = "上一页";
        FCRValues.rightWindowUI[4].rightText = "下一页";
        FCRValues.rightWindowUI[5].rightText = "改造";
        if(selectFixTank == null && selectMode == -1) {
            FCRValues.rightWindowUI[6].rightText = "退出";
        }else {
            FCRValues.rightWindowUI[6].rightText = "返回";
        }
    }

    @Override
    public void clickLeftWindow(int i, UnifyShopSayUIEntity entity) {
        if(entity.leftText.length() > 1 && needServiceNames.size() > 0) {
            if(selectFixTank == null) {
                if(i > 0) {
                    selectFixTank = needServiceNames.get(showFristIndex + i-1);
                    resetUI();
                }
            }else {
                FCItem item = FCRMaster.itemLib.getItemOfID(selectFixTank.itemName);
                if(selectMode == -1){
                    if(i == 1) {
                        FCItem upgradeitem = FCRMaster.itemLib.getItemOfID(item.getUpgradeTarget());
                        if (upgradeitem != null) {
                            int holes = 0;
                            for(int j: item.getCannonHoles()){
                                if(j == 1){
                                    holes++;
                                }
                            }
                            String oldD = item.getChName()+" "+item.getWeight()+"t 承"+item.getCapacity()+"t "+holes+"穴 仓"+
                                    item.getShellCaseNum()+" 防"+item.getDefenseAdd();
                            holes = 0;
                            for(int j: upgradeitem.getCannonHoles()){
                                if(j == 1){
                                    holes++;
                                }
                            }
                            String newD = upgradeitem.getChName()+" "+upgradeitem.getWeight()+"t 承"+upgradeitem.getCapacity()+"t "+holes+"穴 仓"+
                                    upgradeitem.getShellCaseNum()+" 防"+upgradeitem.getDefenseAdd();
                            FCRValues.rightWindowUI[0].leftText = newD;
                            FCRValues.rightWindowUI[1].leftText = oldD;
                            clickItRun = upgradeRun;
                        }
                    }else if(i == 2) {
                        clickItRun = null;
                        selectMode = 1;
                        resetUI();
                    }else if(i == 3) {
                        clickItRun = null;
                        selectMode = 2;
                        resetUI();
                    }
                }else if(selectMode == 1) {
                    clickItRun = null;
                    if(i == 2){
                        if(enableChangeGun1Hole) {
                            selectholeNum = 0;
                            clickItRun = changHolesRun;
                            FCRValues.rightWindowUI[0].leftText = "Tips:主炮穴占重"+ FCRConstants.GUN1_hole_weight+"t";
                        }
                    }else if(i==3){
                        if(enableChangeGun2Hole) {
                            selectholeNum = 1;
                            clickItRun = changHolesRun;
                            FCRValues.rightWindowUI[0].leftText = "Tips:副炮穴占重"+ FCRConstants.GUN2_hole_weight+"t";
                        }
                    }else if(i==4){
                        if(enableChangeSEHole) {
                            selectholeNum = 2;
                            clickItRun = changHolesRun;
                            FCRValues.rightWindowUI[0].leftText = "Tips:SE穴占重"+ FCRConstants.SE_hole_weight+"t";
                        }
                    }
                }else if(selectMode == 2) {
                    if(i == 2){
                        if(item.getShellCaseNum() < 8) {
                            clickItRun = addShellHoleRun;
                            FCRValues.rightWindowUI[0].leftText = "增加弹仓加重"+FCRConstants.SHELL_hole_weight+"t";
                        }else {
                            FCRValues.rightWindowUI[0].leftText = "弹仓穴数已经最大";
                        }
                    }else if(i==3){
                        if(item.getShellCaseNum() > 0) {
                            clickItRun = delShellHoleRun;
                            FCRValues.rightWindowUI[0].leftText = "减少弹仓减轻"+FCRConstants.SHELL_hole_weight+"t";
                        }else {
                            FCRValues.rightWindowUI[0].leftText = "弹仓穴数已经最小";
                        }
                    }
                }
            }
        }
    }

    public int selectholeNum = 0;
    private Runnable changHolesRun = new Runnable() {

        @Override
        public void run() {
            FCItem item = FCRMaster.itemLib.getItemOfID(selectFixTank.itemName);
            int state = item.getCannonHoles()[selectholeNum];
            int price = state ==1?CloseHolePrice:OpenHolePrice;
            if(player1.getMoney() >= price) {
                player1.setMoney(player1.getMoney() - price);
                item.getCannonHoles()[selectholeNum] = state == 1 ? 0 : 1;
                selectFixTank.tank.reInitShuxing();
                resetUI();
            }
        }
    };

    private Runnable upgradeRun = new Runnable() {
        @Override
        public void run() {
            FCItem item = FCRMaster.itemLib.getItemOfID(selectFixTank.itemName);
            FCItem upgradeitem = FCRMaster.itemLib.getItemOfID(item.getUpgradeTarget());
            if(player1.getMoney() >= selectFixTank.needFixMoney){
                selectFixTank.tank.setChassisID(gameSave.checkTankItem(upgradeitem.getName()));//生成新的克隆体
                selectFixTank.tank.reInitShuxing();
                //删除旧的克隆体
                gameSave.delTankItemClone(selectFixTank.itemName);
                player1.setMoney(player1.getMoney() - selectFixTank.needFixMoney);
                needServiceNames.remove(selectFixTank);
                checkAndAdd(selectFixTank.tank);
                selectFixTank = null;
                selectMode = -1;//回到首页
                setEndI(Math.min(needServiceNames.size(), showFristIndex + 6));
                resetUI();

                FCRValues.rightWindowUI[1].leftText = "升级完毕，请继续";
            } else {
                FCRValues.rightWindowUI[2].leftText = "金币不足.";
            }
        }
    };

    private Runnable addShellHoleRun = new Runnable() {
        @Override
        public void run() {
            FCItem item = FCRMaster.itemLib.getItemOfID(selectFixTank.itemName);
            if(player1.getMoney() >= AddShellHole) {
                player1.setMoney(player1.getMoney() - AddShellHole);
                item.setShellCaseNum(Math.min(8, item.getShellCaseNum() + 1));
                item.getShellCount()[item.getShellCaseNum()-1] = 0;
                item.getShellCaseType()[item.getShellCaseNum()-1] = "null";
                selectFixTank.tank.reInitShuxing();
                resetUI();

            }else {
                FCRValues.rightWindowUI[2].leftText = "金币不足.";
            }
        }
    };

    private Runnable delShellHoleRun = new Runnable() {
        @Override
        public void run() {
            FCItem item = FCRMaster.itemLib.getItemOfID(selectFixTank.itemName);
            if(player1.getMoney() >= delShellHole) {
                player1.setMoney(player1.getMoney() - AddShellHole);
                item.setShellCaseNum(Math.min(8, item.getShellCaseNum() - 1));
                item.getShellCount()[item.getShellCaseNum()-1] = 0;
                item.getShellCaseType()[item.getShellCaseNum()-1] = "null";
                selectFixTank.tank.reInitShuxing();
                resetUI();

            }else {
                FCRValues.rightWindowUI[2].leftText = "金币不足.";
            }
        }
    };
    @Override
    public void clickRightWindow(int i, UnifyShopSayUIEntity entity) {
        switch (i) {
            case 3:
                if(selectFixTank == null) {
                    if (showFristIndex >= 6) {
                        showFristIndex -= 6;
                        setEndI(Math.min(needServiceNames.size(), showFristIndex + 6));
                        resetUI();
                    }
                }
                break;
            case 4:
                if(selectFixTank == null) {
                    if (endI < needServiceNames.size()) {
                        showFristIndex += 6;
                        setEndI(Math.min(needServiceNames.size(), showFristIndex + 6));
                        resetUI();
                    }
                }
                break;
            case 5:
                if(selectFixTank != null){
                    if(clickItRun != null){
                        clickItRun.run();
                    }
                }
                break;
            case 6:
                clickItRun = null;
                if(selectFixTank == null) {
                    fcv.setOpenSayShop(false);
                    FCRMaster.fcv.setOpenSay(false);
                    FCRMaster.fcv.setSayNpcName(null);
                    FCRMaster.fcv.setSayNpcTextNum(0);
                }else {
                    setEndI(Math.min(needServiceNames.size(), showFristIndex + 6));
                    if(selectMode != -1){
                        selectMode = -1;
                    }else {
                        selectFixTank = null;
                    }
                    resetUI();
                }
                break;
        }
    }

}
